<?php
include "../site_specific/project.php";
include "../includes/constants.php";
include "../site_specific/external_constants.php";
include "../includes/phpmailer/class.phpmailer.php";
include "../includes/phpmailer/class.smtp.php";
include "../includes/utilities.php";
include "../includes/pages.php";
include "../includes/database.php";
include "../includes/users.php";
include "../includes/date.php";
include "../includes/text.php";
include "../includes/files.php";

# Establish database connection
establishDatabaseConnection();

$get_value = $_GET["value"];

$get_id = $_GET["id"];

$return_content = ""; 
	
$sql_delete_tags = "Delete ".$PROJECT_DB_TABLES["tags"]." FROM ".$PROJECT_DB_TABLES["tags"]." LEFT JOIN ".$PROJECT_DB_TABLES["blogtag"]." ON ".$PROJECT_DB_TABLES["tags"].".t_index = ".$PROJECT_DB_TABLES["blogtag"].".bt_tag WHERE ".$PROJECT_DB_TABLES["tags"].".t_tag=\"".$get_value."\"";

try{
	executeSQLQuery($sql_delete_tags);
}
catch(Exception $e){
	
	echo "Delete is not sucessful";
}


$sql_tags = "SELECT DISTINCT t_tag FROM ".$PROJECT_DB_TABLES["tags"]." order by t_tag";
					  
$sql_selected_tags = "SELECT DISTINCT t_tag FROM ".$PROJECT_DB_TABLES["blogtag"]." bt JOIN ".$PROJECT_DB_TABLES["blogs"]." b ON b.b_index = bt.bt_blog JOIN ".$PROJECT_DB_TABLES["tags"]." t ON bt.bt_tag = t.t_index WHERE b_id=\"".$get_id."\"";
					  
#Get the data result
$data_tags = getRecord($sql_tags);
  
$data_selected_tags = getRecord($sql_selected_tags);
  
$return_content .="<div id=\"div_selected_tags\">";
								  
foreach($data_tags as $tag_keys =>$tag_values){
		  
	if(in_array($tag_values, $data_selected_tags)){
		
		$return_content .= "&nbsp;&nbsp;".$tag_values["t_tag"].":<input type=\"checkbox\" id=\"".$tag_values["t_tag"]."\" name=\"".$tag_values["t_tag"]."\" value=\"".$tag_values["t_tag"]."\" onchange=\"saveTags('".$tag_values["t_tag"]."','".$get_id."')\" checked=\"checked\" />";
	}
	else{
		
		$return_content .= "&nbsp;&nbsp;".$tag_values["t_tag"].":<input type=\"checkbox\" id=\"".$tag_values["t_tag"]."\" name=\"".$tag_values["t_tag"]."\" value=\"".$tag_values["t_tag"]."\" onchange=\"saveTags('".$tag_values["t_tag"]."','".$get_id."')\" />";
	}
} 

$return_content .="</div>";

$return_content .= "%%%###";

$count = 0; 

foreach($data_tags as $data_tags_values){
	
	if($count == 0){	  
		$return_content.= $data_tags_values["t_tag"];
	}
	else{
		$return_content.= "@@@!!!".$data_tags_values["t_tag"];
	}
	$count ++;
}

echo $return_content;
					  

?>